package cn.silver.module.authority.bean;

import cn.silver.framework.core.utils.StringUtils;
import cn.silver.framework.mybatis.bean.BusBean;
import cn.silver.module.authority.domain.SysClient;
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty;
import io.github.linpeilie.annotations.AutoMapper;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.collections4.CollectionUtils;

import java.io.Serial;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 授权管理业务对象 sys_client
 *
 * @author Silver Zhou
 */
@Data
@ExcelIgnoreUnannotated
@EqualsAndHashCode(callSuper = true)
@AutoMapper(target = SysClient.class)
@Schema(name = "ClientModel", title = "客户端授权管理对象")
public class ClientBean extends BusBean implements Serializable {

    @Serial
    private static final long serialVersionUID = 1L;

    /**
     * 设备类型
     */
    @Schema(title = "设备类型")
    @ExcelProperty(value = "设备类型")
    private String deviceType;

    @Schema(title = "授权类型")
    private String grantType;

    /**
     * 授权类型
     */
    @Schema(title = "授权类型")
    @ExcelProperty(value = "授权类型")
    private List<String> grantTypes;

    /**
     * 客户端秘钥
     */
    @Schema(title = "验证码类型")
    @ExcelProperty(value = "验证码类型")
    private String captcha;

    /**
     * 是否开放注册入口
     */
    @Schema(title = "是否开放注册入口")
    private Boolean register;

    /**
     * token活跃超时时间
     */
    @Schema(title = "活跃超时时间")
    @ExcelProperty(value = "token活跃超时时间")
    private Long activeTimeout;

    /**
     * token固定超时时间
     */
    @Schema(title = "固定超时时间")
    @ExcelProperty(value = "token固定超时时间")
    private Long timeout;

    /**
     * 客户端秘钥
     */
    @Schema(title = "客户端秘钥")
    private String secret;

    @Schema(title = "客户端公钥")
    private String publicKey;
    @Schema(title = "客户端公钥")
    private String privateKey;
    @Schema(title = "客户端签名")
    private String signatrue;

    public List<String> getGrantTypes() {
        if (CollectionUtils.isNotEmpty(this.grantTypes)) {
            return this.grantTypes;
        } else if (StringUtils.isNotBlank(this.grantType)) {
            return Arrays.stream(this.grantType.split(StringUtils.SEPARATOR)).toList();
        } else {
            return Collections.emptyList();
        }
    }
}
